package cn.schoolwow.util.module.crypto.cipher.aes.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;

public class GenerateSecretKeyFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        int keyLength = flowContext.checkData("keyLength", int.class);

        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(keyLength);
        SecretKey secretKey = keyGenerator.generateKey();
        String secretKeyString = Base64.getEncoder().encodeToString(secretKey.getEncoded());
        flowContext.putReturnData("secretKey", secretKeyString);
    }

    @Override
    public String name() {
        return "生成AES格式密钥";
    }
}
